import 'package:url_launcher/url_launcher.dart';

class UrlLaunchUtils {
  /// 工厂模式
  factory UrlLaunchUtils() => _getInstance();

  /// 单例
  static UrlLaunchUtils? get instance => _getInstance();
  static late UrlLaunchUtils? _instance;

  UrlLaunchUtils._internal();

  static UrlLaunchUtils _getInstance() => _instance ??= UrlLaunchUtils._internal();

  /// 打开浏览器
  Future<void> launchInBrowser(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: false,
        forceWebView: false,
        headers: <String, String>{'my_header_key': 'my_header_value'},
      );
    } else {}
  }

  /// 在webview中打开
  Future<void> launchInWebViewOrVC(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: true,
        forceWebView: true,
        headers: <String, String>{'my_header_key': 'my_header_value'},
      );
    } else {
      throw 'Could not launch $url';
    }
  }

  /// 在webview中打开启动js
  Future<void> launchInWebViewWithJavaScript(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: true,
        forceWebView: true,
        enableJavaScript: true,
      );
    } else {
      throw 'Could not launch $url';
    }
  }

  /// 在webview中打开启动存储
  Future<void> launchInWebViewWithDomStorage(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: true,
        forceWebView: true,
        enableDomStorage: true,
      );
    } else {
      throw 'Could not launch $url';
    }
  }

  /// ios的universal link
  Future<void> launchUniversalLinkIos(String url) async {
    if (await canLaunch(url)) {
      final bool nativeAppLaunchSucceeded = await launch(
        url,
        forceSafariVC: false,
        universalLinksOnly: true,
      );
      if (!nativeAppLaunchSucceeded) {
        await launch(
          url,
          forceSafariVC: true,
        );
      }
    }
  }

  /// 打电话
  Future<void> makePhoneCall(String phone) async {
    final String url = 'tel:$phone';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  /// 前往百度地图驾驶导航
  Future<void> gotoBaiduMapDriving(String address, double longitude, double latitude) async {
    final String url =
        'baidumap://map/direction?destination=name:$address|latlng:$latitude,$longitude&coord_type=bd09ll&mode=driving';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  /// 前往百度地图marker
  Future<void> gotoBaiduMapMarker(String title, String address, double longitude, double latitude) async {
    final String h5Url = Uri(
      scheme: "http",
      host: "api.map.baidu.com",
      path: '/marker',
      queryParameters: {
        'location': '$latitude,$longitude',
        'title': title,
        'output': "html",
        'src': "andr.baidu.openAPIdemo",
      },
    ).toString();

    final String url = Uri(
      scheme: "baidumap",
      path: "map/marker",
      queryParameters: {
        'location': '$latitude,$longitude',
        'title': title,
        'src': "andr.baidu.openAPIdemo",
        'traffic': "on",
      },
    ).toString();

    /// 地址
    // final String url =
    //     'baidumap://map/marker?location=$latitude,$longitude&title=$title&content=$address&traffic=on&src=andr.baidu.openAPIdemo';
    // final String h5Url =
    //     "http://api.map.baidu.com/marker?location=$latitude,$longitude&title=$title&content=$address&output=html&src=andr.baidu.openAPIdemo";

    ///
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      await launchInBrowser(h5Url);
    }
  }
}
